#include <pi_renderdata.h>

void PI_API pi_renderdata_clear(PiRenderData *data)
{
	uint32 i;
	IndexData *iData = &data->iData;
	VertexData *vData = &data->vData;

	if(NULL != iData->data && iData->isIndexCopy)
		pi_free(iData->data);
	if(NULL != vData->position && vData->isPosCopy)
		pi_free(vData->position);
	if(NULL != vData->normal && vData->isNormCopy)
		pi_free(vData->normal);
	if(NULL != vData->diffuse && vData->isDiffCopy)
		pi_free(vData->diffuse);
	for(i = 0; i < MAX_TEXSTAGE_NUM; ++i)
	{
		if(NULL != vData->texCoord[i].coord &&  vData->isTexCoordCopy[i])
			pi_free(vData->texCoord[i].coord);
	}
	pi_memset(data, 0, sizeof(PiRenderData));
}

void PI_API pi_renderdata_copy_index(PiRenderData *data, uint32 idSize, uint32 idNum, void *index)
{
	uint32 size ;
	void *temp = NULL;
	if(0 < idNum && 0 < idSize)
	{
		size = idSize * idNum;
		temp = pi_malloc(size);
		if(NULL != index)
			pi_memcpy(temp, index, size);
	}
	pi_renderdata_set_index(data, idSize, idNum, temp, TRUE);
}

void PI_API pi_renderdata_copy_position(PiRenderData *data, PiVector3 *pos)
{
	uint32 size;
	void *temp = NULL;
	if(NULL != pos)
	{
		size = sizeof(PiVector3) * data->vData.vertexCount;
		temp = pi_malloc(size);
		pi_memcpy(temp, pos, size);
	}
	pi_renderdata_set_position(data, temp, TRUE);
}

void PI_API pi_renderdata_copy_normal(PiRenderData *data, PiVector3 *normal)
{
	uint32 size;
	void *temp = NULL;
	if(NULL != normal)
	{
		size = sizeof(PiVector3) * data->vData.vertexCount;
		temp = pi_malloc(size);
		pi_memcpy(temp, normal, size);
	}
	pi_renderdata_set_normal(data, temp, TRUE);
}

void PI_API pi_renderdata_copy_diffuse(PiRenderData *data, uint32 *diffuse)
{
	uint32 size;
	void *temp = NULL;
	if(NULL != diffuse)
	{
		size = sizeof((*diffuse)) * data->vData.vertexCount;
		temp = pi_malloc(size);
		pi_memcpy(temp, diffuse, size);
	}
	pi_renderdata_set_diffuse(data, temp, TRUE);
}

void PI_API pi_renderdata_copy_texcoord(PiRenderData *data, uint32 stage, ETexCoordType type, float *coord)
{
	uint32 size;
	void *temp = NULL;
	if(NULL != coord)
	{
		switch(type)
		{
		case ETCT_ONE:
			size = 1 * sizeof(float);
			break;
		case ETCT_TWO:
			size = 2 * sizeof(float);
			break;
		case ETCT_THREE:
			size = 3 * sizeof(float);
			break;
		default:
			break;
		}
		if(0 < size)
		{
			size *= data->vData.vertexCount;
			temp = pi_malloc(size);
			pi_memcpy(temp, coord, size);
		}
	}
	pi_renderdata_set_texcoord(data, stage, type, temp, TRUE);
}

void PI_API pi_renderdata_copy(PiRenderData *dst, PiRenderData *src, uint32 diffElemSize)
{
	uint32 i, idSize;
	if(NULL == src)
		return;

	pi_renderdata_init(dst, src->type);
	pi_renderdata_set_numvertex(dst, src->vData.vertexCount);
	
	idSize = (EINDEX_16BIT == src->iData.type) ? sizeof(uint16) : sizeof(uint32);
	pi_renderdata_copy_index(dst, idSize,  src->iData.num, src->iData.data);
	pi_renderdata_copy_position(dst, src->vData.position);
	pi_renderdata_copy_normal(dst, src->vData.normal);
	pi_renderdata_copy_diffuse(dst, src->vData.diffuse);
	for(i = 0; i < MAX_TEXSTAGE_NUM; ++i)
		pi_renderdata_copy_texcoord(dst, i, src->vData.texCoord[i].type, src->vData.texCoord[i].coord);
}

void PI_API pi_renderdata_compute_aabb(PiRenderData *data)
{
	uint32 i;
	PiVector3 *pos = data->vData.position;
	uint32 numVertex = data->vData.vertexCount;
	pi_aabb_init(&data->box);
	for(i = 0; i < numVertex; ++i)
		pi_aabb_add_point(&data->box, &pos[i]);
}